package gui;

import application.Resolution;
import application.StatutBouton;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.plaf.basic.BasicBorders;

/**
 *
 * @author Alexis
 */
public class BoutonMenu extends JButton {

    private Resolution resolution;
    private StatutBouton statutBouton;

    public BoutonMenu(String text, Resolution resolution) {
        super(text);

        this.resolution = resolution;

        initialisation();
    }

    private void initialisation() {
        statutBouton = StatutBouton.NEUTRE;

        setBorder(new BasicBorders.ButtonBorder(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE));

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                statutBouton = StatutBouton.PRESSE;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (statutBouton != StatutBouton.NEUTRE) {
                    statutBouton = StatutBouton.SOURIS_DESSUS;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                statutBouton = StatutBouton.SOURIS_DESSUS;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                statutBouton = StatutBouton.NEUTRE;
            }
        });
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d;
        Font font;
        FontMetrics fontMetrics;
        int largeurTexte, hauteurTexte, fontSize = 30;

        if (resolution == Resolution.R1920x1080) {
            fontSize = 45;
        } else if (resolution == Resolution.R1280x1024) {
            fontSize = 30;
        } else if (resolution == Resolution.R800x600) {
            fontSize = 20;
        }

        g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        font = new Font("Imprint MT Shadow", Font.PLAIN, fontSize);
        fontMetrics = g2d.getFontMetrics(font);
        g2d.setFont(font);

        if (statutBouton == StatutBouton.NEUTRE) {
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, getWidth(), getHeight());

            largeurTexte = fontMetrics.stringWidth(getText());
            hauteurTexte = fontMetrics.getHeight();

            g2d.setColor(Color.WHITE);
            g2d.drawString(getText(), getWidth() / 2 - largeurTexte / 2, getHeight() / 2 + hauteurTexte / 4);
        } else if (statutBouton == StatutBouton.SOURIS_DESSUS) {
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, getWidth(), getHeight());

            largeurTexte = fontMetrics.stringWidth(getText());
            hauteurTexte = fontMetrics.getHeight();

            g2d.setColor(Color.BLACK);
            g2d.drawString(getText(), getWidth() / 2 - largeurTexte / 2, getHeight() / 2 + hauteurTexte / 4);
        } else if (statutBouton == StatutBouton.PRESSE) {
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, getWidth(), getHeight());

            largeurTexte = fontMetrics.stringWidth(getText());
            hauteurTexte = fontMetrics.getHeight();

            g2d.setColor(Color.WHITE);
            g2d.drawString(getText(), getWidth() / 2 - largeurTexte / 2, getHeight() / 2 + hauteurTexte / 4);
        }
    }

    public void setNeutre() {
        statutBouton = StatutBouton.NEUTRE;
    }

    public void setResolution(Resolution newResolution) {
        resolution = newResolution;
    }
}
